﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Christmas_Card
{
    class Bell : EventObj
    {
        private Image bellImage;
        private Image[] angleImage;

        private const int locationX = 200;
        private const int locationY = 50;

        private int currentAngle;
        private const int minAngle = -15;
        private const int maxAngle = 15;
        public bool positiveDirection;

        private Timer clock;
        private const int interval = 100;

        private PictureBox pb;

        private Panel parentPanel;

        public Bell(Panel _parent, int _beginTime)
            : base(_beginTime)
        {
            positiveDirection = true;
            //bellImage = new Bitmap("D:\\starBell.png");
            bellImage = new Bitmap(global::Christmas_Card.Properties.Resources.Bell);

            parentPanel = _parent;

            pb = new PictureBox();
            pb.Location = new Point(locationX, locationY);
            pb.SizeMode = PictureBoxSizeMode.AutoSize;

            clock = new Timer();
            clock.Tick += new EventHandler(clock_Tick);
            clock.Interval = interval;

            initBellImages();
        }

        private void initBellImages()
        {
            angleImage = new Image[maxAngle - minAngle + 1];
            for (int i = minAngle; i <= maxAngle; i++)
            {
                angleImage[i - minAngle] = rotateImage((Bitmap)bellImage, i);
            }

            currentAngle = minAngle;
            pb.Image = angleImage[currentAngle - minAngle];

            ((Scenario)parentPanel).drawingObj.Add(pb);
        }

        void clock_Tick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            if ((currentAngle == minAngle) || (currentAngle == maxAngle))
                positiveDirection = !positiveDirection;

            if (positiveDirection == true)
            {
                currentAngle--;
            }
            else
            {
                currentAngle++;
            }

            pb.Image = angleImage[currentAngle - minAngle]; pb.SizeMode = PictureBoxSizeMode.AutoSize;
        }

        public override void perform()
        {
            //throw new NotImplementedException();
            clock.Start();
        }

        private Bitmap rotateImage(Bitmap b, float angle)
        {
            //create a new empty bitmap to hold rotated image
            Bitmap returnBitmap = new Bitmap(b.Width*2, b.Height*2);
            //make a graphics object from the empty bitmap
            Graphics g = Graphics.FromImage(returnBitmap);
            //move rotation point to center of image
            g.TranslateTransform((float)b.Width / 2, 0);
            //rotate
            g.RotateTransform(angle);
            //move image back
            g.TranslateTransform(-(float)b.Width / 2, 0);
            //draw passed in image onto graphics object
            g.DrawImage(b, new Point(0, 0));
            return returnBitmap;
        }
    }
}
